package com.xauto.pioneer.appuser.activity.mine.user;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xauto.pioneer.appuser.MyApplication;
import com.xauto.pioneer.appuser.R;
import com.xauto.pioneer.appuser.activity.BaseActivity;
import com.xauto.pioneer.appuser.activity.common.TakePhotoActivity;
import com.xauto.pioneer.appuser.activity.common.TakeUserPhotoActivity;
import com.xauto.pioneer.appuser.activity.common.WebViewActivity;
import com.xauto.pioneer.appuser.http.BaseLHttpHandler;
import com.xauto.pioneer.appuser.http.Constants;
import com.xauto.pioneer.appuser.http.LHttpLib;
import com.xauto.pioneer.appuser.interfaces.MemberCallBack;
import com.xauto.pioneer.appuser.model.Image;
import com.xauto.pioneer.appuser.model.JSONStatus;
import com.xauto.pioneer.appuser.model.Member;
import com.xauto.pioneer.appuser.util.DataCheckUtil;
import com.xauto.pioneer.appuser.util.DataLoadUtil;
import com.xauto.pioneer.appuser.util.StringUtils;
import com.xauto.pioneer.appuser.view.PromptDialog;
import com.xauto.pioneer.appuser.view.TopBar;

import butterknife.Bind;
import butterknife.OnClick;

import static com.xauto.pioneer.appuser.activity.common.TakePhotoActivity.DO_BIG_IMAGE;
import static com.xauto.pioneer.appuser.activity.common.TakePhotoActivity.UPLOAD_IMG;

/***
 * 个人中心设置页面
 * @author Lemon Sun 12 Feb 17:50
 */
public class MineSettingActivity extends BaseActivity {

    @Bind(R.id.topBar) TopBar topBar;
    /** 显示用户头像*/
    @Bind(R.id.ivUserSetAvatar) ImageView ivUserSetAvatar;
    /** 显示用户昵称*/
    @Bind(R.id.tvUserSetNickname) TextView tvUserSetNickname;
    /** 显示用户手机号*/
    @Bind(R.id.tvUserSetMobile) TextView tvUserSetMobile;
    /** 显示身份证信息是否验证 @string/user_set_card_ver -->点击验证 | @string/user_set_card_verified --> 已验证*/
    @Bind(R.id.tvUserSetIdCard) TextView tvUserSetIdCard;
    /** 显示驾驶证信息是否验证*/
    @Bind(R.id.tvUserSetCarCard) TextView tvUserSetCarCard;
    /** 显示银行卡*/
    @Bind(R.id.tvUserBankCard) TextView tvUserBankCard;
    /** 显示会员等级*/
    @Bind(R.id.tvUserSetVip) TextView tvUserSetVip;
    private PromptDialog promptDialog;

    private ImageLoader imageLoader;
    private DisplayImageOptions options;
//    private String avatarAllUrl;

    private boolean isEditSuccess;
    private Member member;

    @Override
    protected int getContentView() {
        return R.layout.ac_user_setting;
    }

    @Override
    protected void initView() {
        topBar.showView(TopBar.TITLE, TopBar.LEFT_IMAGE_BTN);
        topBar.setText(TopBar.TITLE, getString(R.string.top_bar_user_set));
        topBar.setImage(TopBar.LEFT_IMAGE_BTN, R.mipmap.ic_common_left);
        topBar.setLeftOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        promptDialog = new PromptDialog(mContext, getString(R.string.notice), getString(R.string.text_not_network),
                getString(R.string.to_setting), getString(R.string.logout),  getString(R.string.dialogpic_notice),new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //去设置
                promptDialog.dismiss();
            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //退出
                promptDialog.dismiss();
            }
        });
    }

    @Override
    protected void initData() {
        imageLoader = ImageLoader.getInstance();
        options = MyApplication.getDisplayImageOptions(mContext, 35, R.mipmap.ic_default_user_avatar);

    }

    @Override
    protected void onResume() {
        super.onResume();
        member = DataCheckUtil.getMemberObj(mContext);
        setData(member);
    }

    private void setData(Member meb) {
        member = meb;
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                imageLoader.displayImage(member.icon_image.thumb, ivUserSetAvatar, options);
                tvUserSetNickname.setText(!StringUtils.isBlank(member.member_name) ? member.member_name : "请输入姓名");
                tvUserSetMobile.setText(StringUtils.hideMobile(member.mobile));

                // @string/user_set_card_ver -->点击验证 | user_set_card_verified  --> 已验证 | user_set_card_verify --> 认证中
                //身份证信息&&手持身份证信息都通过才算验证成功
                //身份证信息
                int idNumberAuthStatus = Integer.parseInt(member.member_auth_info.id_number_auth_status);
                //手持身份证信息
                int idNumberWithPeopleAuthStatus = Integer.parseInt(member.member_auth_info.id_number_with_people_auth_status);
                if(idNumberAuthStatus == 1 && idNumberWithPeopleAuthStatus == 1) {
                    tvUserSetIdCard.setText(getString(R.string.user_set_card_verified));
                }else if(idNumberAuthStatus == 0 && idNumberWithPeopleAuthStatus == 0) {
                    tvUserSetIdCard.setText(getString(R.string.user_set_card_ver));
                }else{
                    tvUserSetIdCard.setText(getString(R.string.user_set_card_verify));
                }
                //驾驶证信息
                if(member.member_auth_info.driving_license_auth_status.equals("1")) {
                    tvUserSetCarCard.setText(getString(R.string.user_set_card_verified));
                }
                if(member.member_auth_info.driving_license_auth_status.equals("0")) {
                    tvUserSetCarCard.setText(getString(R.string.user_set_card_ver));
                }
                if(member.member_auth_info.driving_license_auth_status.equals("2")) {
                    tvUserSetCarCard.setText(getString(R.string.user_set_card_verify));
                }
                //银行卡认证
                Log.e("银行认证",member.member_auth_info.id_number_bank_status);
                if(member.member_auth_info.id_number_bank_status.equals("1")) {
                    tvUserBankCard.setText(getString(R.string.user_set_card_verified));
                }
                if(member.member_auth_info.id_number_bank_status.equals("0")) {
                    tvUserBankCard.setText(getString(R.string.user_set_card_ver));
                }
                if(member.member_auth_info.id_number_bank_status.equals("2")) {
                    tvUserBankCard.setText(getString(R.string.user_set_card_verify));
                }
                //等级设置
                if (StringUtils.isBlank(member.member_level_info.member_level_name)){
                    tvUserSetVip.setText("0级");
                }else {
                    tvUserSetVip.setText(member.member_level_info.member_level_name+"级");
                }

            }
        });
    }

    @OnClick({R.id.llUserSetAvatar, R.id.llUserSetNickname, R.id.llUserSetMobile,
            R.id.tvUserSetIdCard, R.id.tvUserSetCarCard, R.id.llUserSetVip,
    R.id.tvUserBankCard})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.llUserSetAvatar:
                //用户头像
                startActivityForResult(TakeUserPhotoActivity.getTakePhotoStartIntent(mContext,
                        DO_BIG_IMAGE,  1, 1, 640, 640), UPLOAD_IMG);
                break;
            case R.id.llUserSetNickname:
                //昵称
                Intent intent = new Intent(mContext, EditNicknameActivity.class);
                intent.putExtra(Constants.EDIT_TYPE_MEMBER_NAME, member.member_name);
                startActivityForResult(intent, Constants.REQUEST_EDIT);
                break;
            case R.id.llUserSetMobile:
                //手机号
                Intent intent1 = new Intent(mContext, EditMobileActivity.class);
                intent1.putExtra(Constants.EDIT_TYPE_MOBILE, member.mobile);
                startActivityForResult(intent1, Constants.REQUEST_EDIT);
                break;
            case R.id.tvUserSetIdCard:
                //身份证信息 todo 需要先判断当前验证状态： 未上传-跳转去验证页面；审核中-跳转公共提示页面；已通过-带参跳转去验证页面
                //身份证信息
                int idNumberAuthStatus = Integer.parseInt(member.member_auth_info.id_number_auth_status);
                //手持身份证信息
                int idNumberWithPeopleAuthStatus = Integer.parseInt(member.member_auth_info.id_number_with_people_auth_status);
                IdentificationActivity.startAction(mContext,idNumberAuthStatus,idNumberWithPeopleAuthStatus);
                break;
            case R.id.tvUserSetCarCard:
                //驾驶证信息
                int driverAuthStatus = Integer.parseInt(member.member_auth_info.driving_license_auth_status);
                DriverInformationActivity.startAction(mContext,driverAuthStatus);
                break;
            case R.id.tvUserBankCard:
                //银行卡
                int id_number_bank_status = Integer.parseInt(member.member_auth_info.id_number_bank_status);
                BnakInformationActivity.startAction(mContext,id_number_bank_status);
                break;
            case R.id.llUserSetVip:
                //会员等级
                WebViewActivity.startAction(mContext,4);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 101 && resultCode == TakePhotoActivity.UPLOAD_SUCCESS) {
            if(data != null && data.getExtras() != null) {
//                Uri avatarUri = data.getExtras().getParcelable("PATH");
//                assert avatarUri != null;
                loadEditImage(data.getExtras().getString("PATH"));
            }
        }
        if(requestCode == Constants.REQUEST_EDIT && resultCode == Constants.RESULT_EDIT_SUCCESS) {
            refreshMemberData();
        }
    }
    private void refreshMemberData(){
        DataLoadUtil.refreshMemberData(mContext, new MemberCallBack() {
            @Override
            public void getMember(Member member) {
                setData(member);
            }
        });
    }
    private void loadEditImage(String path) {
        new LHttpLib().editImage(mContext, memberId, Constants.IMAGE_TYPE_AVATAR, path, new BaseLHttpHandler(mContext, true, true) {
            @Override
            public void onSuccess(JSONStatus jsonStatus) {
                super.onSuccess(jsonStatus);
                final Image image = new Image().parse(jsonStatus.data.optJSONObject("image_info"));
//                avatarAllUrl = image.source;
                loadEditMember(Constants.IMAGE_TYPE_AVATAR, image.source);
            }
        });
    }

    void loadEditMember(String type, String value) {
        new LHttpLib().editMemberInfo(mContext, memberId, type, value, "", new BaseLHttpHandler(mContext, true, true) {

            @Override
            public void onSuccess(JSONStatus jsonStatus) {
                super.onSuccess(jsonStatus);
                refreshMemberData();
                isEditSuccess = true;
            }
        });
    }

    @Override
    public void onBackPressed() {
        if(isEditSuccess) {
            setResult(Constants.RESULT_EDIT_SUCCESS);
        }
        finish();
    }
}
